//
//  SelectView.m
//  ChatForHot
//
//  Created by FLY on 2018/10/11.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "SelectView.h"


@interface SelectView()<UIPickerViewDataSource,UIPickerViewDelegate>


@end

@implementation SelectView

- (instancetype)init{
    if (self = [super init]) {
        [self addSubview:self.backview];
        [self addSubview:self.titleLab];
        [self addSubview:self.pickerview];
        [self addSubview:self.cancelBtn];
        [self addSubview:self.okBtn];
        [self addSubview:self.line1];
        [self addSubview:self.line2];
        [self addSubview:self.line3];
    }
    return self;
}
#pragma mark - Views
- (UIView *)backview{
    if (_backview == nil) {
        _backview = [[UIView alloc] init];
        _backview.backgroundColor = [UIColor whiteColor];
        _backview.layer.cornerRadius = 8;
        _backview.clipsToBounds = YES;
    }
    return _backview;
}
- (UILabel *)titleLab{
    if (_titleLab == nil) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.textColor = HOTCHATrgb(51, 51, 51);
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}
- (UIView *)line1{
    if (_line1 == nil) {
        _line1 = [[UIView alloc] init];
        _line1.backgroundColor = HOTCHATrgb(230, 230, 230);
    }
    return _line1;
}
- (UIView *)line2{
    if (_line2 == nil) {
        _line2 = [[UIView alloc] init];
        _line2.backgroundColor = HOTCHATrgb(230, 230, 230);
    }
    return _line2;
}
- (UIView *)line3{
    if (_line3 == nil) {
        _line3 = [[UIView alloc] init];
        _line3.backgroundColor = HOTCHATrgb(230, 230, 230);
    }
    return _line3;
}
- (UIPickerView *)pickerview{
    if (_pickerview == nil) {
        _pickerview = [[UIPickerView alloc] init];
        _pickerview.dataSource = self;
        _pickerview.delegate = self;
    }
    return _pickerview;
}
- (UIButton *)cancelBtn{
    if (_cancelBtn == nil) {
        _cancelBtn = [[UIButton alloc] init];
        [_cancelBtn setTitle:NSLocalizedString(@"CANCELBUTTON", nil) forState:(UIControlStateNormal)];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        UIColor *color = HOTCHATrgb(51, 51, 51);
        [_cancelBtn setTitleColor:color forState:(UIControlStateNormal)];
        UIColor *color2 = HOTCHATrgba(51, 51, 51,0.7);
        [_cancelBtn setTitleColor:color2 forState:(UIControlStateHighlighted)];
        _cancelBtn.backgroundColor = [UIColor whiteColor];
        
        [_cancelBtn addTarget:self action:@selector(HOTCHATclcikCancel) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _cancelBtn;
}
- (UIButton *)okBtn{
    if (_okBtn == nil) {
        _okBtn = [[UIButton alloc] init];
        [_okBtn setTitle:NSLocalizedString(@"OKBUTTON", nil) forState:(UIControlStateNormal)];
        _okBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        UIColor *color = HOTCHATrgb(250, 96, 85);
        [_okBtn setTitleColor:color forState:(UIControlStateNormal)];
        UIColor *color2 = HOTCHATrgba(250, 96, 85,0.6);
        [_okBtn setTitleColor:color2 forState:(UIControlStateHighlighted)];
        _okBtn.backgroundColor = [UIColor whiteColor];
        [_okBtn addTarget:self action:@selector(HOTCHATclcikOk) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _okBtn;
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (self.selectedType == HeightMsg) {
        return 2;
    }
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (self.selectedType == HeightMsg) {
        if (component == 0) {
            return 4;
        }
        return 12;
    }
    return self.dataArr.count;
}
#pragma mark - UIPickerViewDelegate

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    if (view == nil) {
        view = [[UILabel alloc] init];
        NSString *textValue;
        if (self.selectedType == HeightMsg) {
            if (component == 0) {
                textValue = [NSString stringWithFormat:@"%ld",row + 4];
            }else{
                textValue = [NSString stringWithFormat:@"%ld",(long)row];
            }
        }else{
            id obj = [self.dataArr objectAtIndex:row];
            if ([obj isKindOfClass:[TypeModel class]]) {
                TypeModel *listmodel = obj;
                textValue = listmodel.name;
            }else{
                textValue = obj;
            }
        }
        ((UILabel *)view).text = textValue;
        ((UILabel *)view).textAlignment = NSTextAlignmentCenter;
        ((UILabel *)view).font = [UIFont systemFontOfSize:18];
        ((UILabel *)view).textColor = HOTCHATrgb(26, 26, 26);
    }
    return view;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
}

#pragma mark - Actions

- (void)setMsgType:(MsgType)type model:(UserInfoModel *)vlue{
    self.selectedType = type;
    [self resetViews:vlue];
}
- (NSMutableArray *)dataArr{
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (void)resetViews:(UserInfoModel *)model{
    [self.dataArr removeAllObjects];
    NSInteger selectRow;
    switch (self.selectedType) {
        case AgeMsg://1-100=18
        {
            self.titleLab.text = NSLocalizedString(@"profile1", nil);
            for (int i = 18; i <= 100; i ++) {
                [self.dataArr addObject:[NSString stringWithFormat:@"%d",i]];
            }
            
            selectRow = [self.dataArr indexOfObject:model.age];
            if (selectRow >=0 && selectRow < self.dataArr.count) {
                
            }else{
                selectRow = 0;
            }
        }
            break;
        case HeightMsg://110-210=175
        {
            self.titleLab.text = NSLocalizedString(@"profile2", nil);
            selectRow = model.minFeet.integerValue - 1;

        }
            break;
        case WeightMsg://30-200=65
        {
            self.titleLab.text = NSLocalizedString(@"profile3", nil);
            
            
            [self.dataArr addObjectsFromArray:[UserInfoModel HOTCHATtypeList]];
            
            selectRow = model.weight.integerValue - 1;
            if (selectRow < 0 || selectRow >= self.dataArr.count) {
                selectRow = 0;
            }
        }
            break;
        case StarMsg:
        {
           self.titleLab.text = NSLocalizedString(@"profile4", nil);
            [self.dataArr addObjectsFromArray:[UserInfoModel HOTCHATconstellationArray]];
            
            selectRow = model.constellation.integerValue - 1;
            if (selectRow < 0 || selectRow >= self.dataArr.count) {
                selectRow = 0;
            }
        }
            break;
        case IncomeMsg:
        {
            self.titleLab.text = NSLocalizedString(@"profile5", nil);
            
           [self.dataArr addObjectsFromArray:[UserInfoModel HOTCHATincomeArray]];
            selectRow = [self.dataArr indexOfObject:model.income];
            if (selectRow >= 0 && selectRow < self.dataArr.count) {
                
            }else{
                selectRow = 0;
            }
        }
            break;
        case JobMsg:
        {
            self.titleLab.text = NSLocalizedString(@"profile6", nil);
            [self.dataArr addObjectsFromArray:[UserInfoModel HOTCHATjobArray]];
            
            selectRow = model.occupation.integerValue - 1;
            if (selectRow < 0 || selectRow >= self.dataArr.count) {
                selectRow = 0;
            }
        }
            break;
            
        default:
            break;
    }
    
    [self.pickerview reloadAllComponents];
    [self.pickerview selectRow:selectRow inComponent:0 animated:NO];
    if (self.selectedType == HeightMsg) {
        NSInteger com2 = model.maxFeet.integerValue;
        if (com2 < 0 || com2 >= 12) {
            com2 = 9;
        }
        [self.pickerview selectRow:com2 inComponent:1 animated:NO];
    }
}

- (void)HOTCHATshowTheView:(UIView *)perView{
    if (self.superview == nil) {
        [perView addSubview:self];
        self.backgroundColor = [UIColor clearColor];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(perView);
        }];
        
        [self setNeedsLayout];
        [self layoutIfNeeded];
        
        [self performSelector:@selector(HOTCHATmoveAction:) withObject:@"1" afterDelay:0.01];
    }
}
- (void)HOTCHATmoveAction:(NSString *)isOpen{
    if ([isOpen isEqualToString:@"1"]) {
        self.isOpen = YES;
    }else{
        self.isOpen = NO;
    }
    
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.25 animations:^{
        if (isOpen) {
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        }else{
            self.backgroundColor = [UIColor clearColor];
        }
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            if (!self.isOpen) {
                [self removeFromSuperview];
            }
        }
    }];
}
- (void)updateConstraints{
    [super updateConstraints];
    
    [self.backview mas_updateConstraints:^(MASConstraintMaker *make) {
        if (self.isOpen) {
            make.top.equalTo(self.mas_bottom).offset(-330);
        }else{
            make.top.equalTo(self.mas_bottom);
        }
        
        make.left.right.equalTo(self);
        make.height.mas_equalTo(330);
    }];
    [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.backview);
        make.height.mas_equalTo(50);
    }];
    [self.line1 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.backview);
        make.top.equalTo(self.backview).offset(50);
        make.height.mas_equalTo(0.5);
    }];
    [self.line2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.backview);
        make.bottom.equalTo(self.backview).offset(-50);
        make.height.mas_equalTo(0.5);
    }];
    [self.pickerview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line1);
        make.left.right.equalTo(self.backview);
        make.bottom.equalTo(self.line2);
    }];
    [self.line3 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line2);
        make.bottom.equalTo(self.backview);
        make.width.mas_equalTo(0.5);
        make.centerX.equalTo(self.backview);
    }];
    [self.cancelBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backview);
        make.top.equalTo(self.line2.mas_bottom);
        make.right.equalTo(self.line3.mas_left);
        make.bottom.equalTo(self.backview);
    }];
    [self.okBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.line3.mas_right);
        make.top.equalTo(self.line2.mas_bottom);
        make.right.equalTo(self.backview);
        make.bottom.equalTo(self.backview);
    }];
}
- (void)HOTCHATcloseTheView{
    if (self.superview == nil) {
        
    }else{
        [self HOTCHATmoveAction:@"0"];
    }
}
- (void)HOTCHATclcikCancel{
    [self HOTCHATcloseTheView];
}
- (void)HOTCHATclcikOk{
    [self HOTCHATcloseTheView];
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATselectedMsg:type:num1:num2:)]) {
        NSInteger index = [self.pickerview selectedRowInComponent:0];
        if (index >= 0 && index < self.dataArr.count) {
            NSString *msg;
            id object = [self.dataArr objectAtIndex:index];
            if ([object isKindOfClass:[TypeModel class]]) {
                TypeModel *listmodel = object;
                msg = listmodel.typeID;
            }else {
                msg = object;
            }
            
            [self.delegate HOTCHATselectedMsg:msg type:self.selectedType num1:0 num2:0];
        }
        else if (self.selectedType == HeightMsg ){
            NSInteger index1 = [self.pickerview selectedRowInComponent:0] + 4;
            NSInteger index2 = [self.pickerview selectedRowInComponent:1];
            
            NSInteger value = index1 * 30.48 + index2 * 2.54;
            NSString *msg = [NSNumber numberWithInteger:value].stringValue;
            NSLog(@"%@cm",msg);
            [self.delegate HOTCHATselectedMsg:msg type:self.selectedType num1:index1 num2:index2];
        }
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if (CGRectContainsPoint(self.backview.frame, point)) {
        return;
    }
    [self HOTCHATcloseTheView];
}

@end
